$(function() {
    $.initPage(eContracFlowDtail.load.start);
});

var eContracFlowDtail = new BaseFrame();

eContracFlowDtail.addBlock('flowTitle', 'flowDetail');

//load module
eContracFlowDtail.load.addStart(function() {
    $.getFlowDetail()
        .then(function(data) {
            _data = $.extend({}, data);
            return $.pageStart(_eLancoSettings);
        })
        .then(function() {
            eContracFlowDtail.load.runTask();
            eContracFlowDtail.event.runTask();
        })
});

eContracFlowDtail.load.addBind('createTitle', function() {
    var currentPage = $.getCurrentPage(_eLancoSettings.pageList);

    $('title').text(currentPage.title);
});

eContracFlowDtail.load.addBind('createNav', function() {
    var navList = $.getUserAuthorityList(_user.roleList, _eLancoSettings.authorityList, _eLancoSettings.pageList);
    $('#logout').on('click', $.logoff);
    $('#block-nav').createNavList(navList).removeClass('dn');
});

eContracFlowDtail.load.flowTitle.addBind('createFlowTitle', function() {
    $('#flow-number').text(_data.formDetail.formNumber);
    $('#flow-type').text("Type:" + _data.formDetail.form.formEnglishName)
});

eContracFlowDtail.load.flowDetail.addBind('createFlowDetail', function() {
    var formDetail = _data.formDetail
    var approvalList = formDetail.approvalList;
    var approvalStep = formDetail.approvalStep;
    var approvalFinalStep = formDetail.approvalFinalStep;
    stepId = approvalList[1] != undefined ? approvalList[1].stepId : null;
    var noBsApprovalStep = formDetail.formId == 2 && stepId == 3;
    for (var i = 0; i < approvalList.length; i++) {
        var approvalItem = approvalList[i];
        var flowItem = eContracFlowDtail.form.getFlowItem(approvalItem.approvalStatus, approvalItem.comment, approvalItem.approvalDate, approvalItem.approver);
        if (approvalItem.approvalStep != approvalFinalStep || noBsApprovalStep) {
            if (approvalItem.approvalStep < approvalStep) {
                if (approvalItem.approvalStatus != _enumApprovalStatus.待审批) {
                    $('#block-flow').append(flowItem);
                }
            } else {
                $('#block-flow').append(flowItem);
            }
        } else {
            if (approvalItem.approvalStatus != _enumApprovalStatus.待审批) {
                $('#block-flow').append(flowItem);
            }
        }
        if (approvalList[i].approvalStatus < 0) {
            break;
        }
    }
});
//event module

//flow module
eContracFlowDtail.form.addEvent('getFlowItem', function(status, comment, date, approver) {
    var statusText = "待审批";
    var statusTextEN = "pending";
    if (status < 0) {
        statusText = "拒绝";
        statusTextEN = "reject";
    }
    if (status > 0) {
        statusText = "同意";
        statusTextEN = "approval";
    }
    var html = $('<div class="flow-item"></div>');
    var flowItemIcon = '<div class="flow-detail-icon flow-detail-' + statusTextEN + '"><img src="../../asstes/images/' + statusTextEN + '.png"></div>';
    var flowStatus = '<div class="flow-detail-status"><img src="../../asstes/images/' + statusTextEN + '.png"><span class="' + $.getFlowDetailClass(status) + '">' + statusText + '</span></div>';
    var flowApprover = '<p class="flow-detail-approver">' + approver.chineseName + '</p>'
    var flowCommon = '<p class="flow-detail-common">' + comment + '</p>';
    var flowDate = '<p class="flow-detail-date">' + $.toDate(date) + '</p>';
    html.append(flowItemIcon, flowStatus, flowApprover, flowCommon, flowDate);
    return html;
});
//save module

//verify module

//message module

//data module